package com.ball.dev.service.impl;

import com.ball.common.dtos.ResponseResult;
import com.ball.common.enums.AppHttpCodeEnum;
import com.ball.dev.entity.po.Post;
import com.ball.dev.entity.po.PostCollect;
import com.ball.dev.entity.po.PostLikes;
import com.ball.dev.mapper.PostCollectMapper;
import com.ball.dev.service.IPostCollectService;
import com.ball.dev.service.IPostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.io.PipedReader;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lou
 * @since 2024-03-27
 */
@Service
@RequiredArgsConstructor
public class PostCollectServiceImpl extends ServiceImpl<PostCollectMapper, PostCollect> implements IPostCollectService {

    private final IPostService postService;
    @Override
    public ResponseResult addPostCollects(PostCollect postCollect) {
        if (postCollect == null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数不能为空");
        }

        // 检查postCollect对象中的关键参数是否合法
        if (postCollect.getPostId() == null &&  postCollect.getUserId() == null && postCollect.getStatus() ==null){
            return   ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不合法");
        }

        // 处理收藏逻辑
        if (postCollect.getStatus() == 0){
            // 尝试根据用户ID和帖子ID查询已存在的收藏记录
            PostCollect postCollect1 = lambdaQuery().eq(PostCollect::getUserId, postCollect.getUserId())
                    .eq(PostCollect::getPostId, postCollect.getPostId())// 根据用户ID和帖子ID查询
                    .one();// 查询结果
            // 如果不存在，则插入新的收藏记录
            if (postCollect1 == null){
                postCollect.setCreateTime(LocalDateTime.now());
                postCollect.setUpdateTime(LocalDateTime.now());
                save(postCollect);
                postService.lambdaUpdate().eq(Post::getId, postCollect.getPostId())// 根据帖子ID更新
                        .setSql("collects = collects + 1").update();// 更新帖子收藏数


                return   ResponseResult.okResult("收藏成功");
            }
            // 如果存在，则更新为收藏状态
            postCollect1.setUpdateTime(LocalDateTime.now());
            postCollect1.setStatus(0);
            updateById(postCollect1);

            postService.lambdaUpdate().eq(Post::getId, postCollect.getPostId())// 根据帖子ID更新
                    .setSql("collects = collects + 1").update();// 更新帖子收藏数
            return   ResponseResult.okResult("收藏成功");
        }
        // 更新为取消收藏状态
        lambdaUpdate()
                .eq(PostCollect::getUserId, postCollect.getUserId())// 根据用户ID和帖子ID更新
                .eq(PostCollect::getPostId, postCollect.getPostId())
                .set(PostCollect::getStatus,1)//     更新为取消收藏状态
                .set(PostCollect::getUpdateTime,LocalDateTime.now())//更新时间
                .update();// 更新数据

        postService.lambdaUpdate().eq(Post::getId, postCollect.getPostId())// 根据帖子ID更新
                .setSql("collects = collects - 1").update();// 更新帖子收藏数
        return ResponseResult.okResult("取消收藏成功");
    }
}
